<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/commons/global.jsp"%>

<table id="notEntityCategoryList" style="height: 100%; width: 100%;">

</table>

<div id="notEntityCategoryListToolBars" style="height: auto">
	<a href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-remove',plain:true"
		onclick="notEntityCategoryList_remove()">删除</a>| <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-add',plain:true"
		onclick="notEntityCategoryList_append()">添加</a>|<a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-edit',plain:true"
		onclick="notEntityCategoryList_edit()">编辑</a> | <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-undo',plain:true"
		onclick="notEntityCategoryList_undo()">取消</a>| <a
		href="javascript:void(0)" class="easyui-linkbutton"
		data-options="iconCls:'icon-save',plain:true"
		onclick="notEntityCategoryList_accept()">保存</a>
	<!-- 		|<a -->
	<!-- 		href="javascript:void(0)" class="easyui-linkbutton" -->
	<!-- 		data-options="iconCls:'fa fa-cog',plain:true" -->
	<!-- 		onclick="notEntityCategoryChildList_config()">子分类管理</a>  -->
	| <input id="notEntityCategoryItemTypeQue" style="width: 100px;" />|<input
		id="notEntityCategoryName" style="width: 200px;" />
</div>
<script type="text/javascript">
	var notEntityCategoryQueryParams = {
		itemType : 0,
		entity : false,
		like_name : ''
	};
	function notEntityCategoryListtreegridReload() {
		try {
			notEntityCategoryQueryParams.itemType = notEntityCategoryItemTypeQue
					.combobox('getValue');
			notEntityCategoryQueryParams.like_name = notEntityCategoryName
					.textbox('getValue');

			notEntityCategoryListtreegrid.treegrid('options').queryParams = notEntityCategoryQueryParams;

			notEntityCategoryListtreegrid.treegrid('reload');
		} catch (e) {
		}
	}
</script>
<script type="text/javascript">
	var notEntityCategoryName = undefined;
	var notEntityCategoryItemTypeQue = undefined;
	var notEntityCategoryEditRow = undefined;
	var notEntityCategoryListtreegrid = undefined;
	$(function() {

		notEntityCategoryName = $('#notEntityCategoryName').textbox({
			buttonText : '名称搜索',
			buttonIcon : 'icon-search',
			buttonAlign : 'right'
		});

		notEntityCategoryName.textbox('button').click(function() {
			// 			if (!notEntityCategoryName.textbox('getValue')) {
			// 				return;
			// 			}
			notEntityCategoryListtreegridReload();
		});

		notEntityCategoryItemTypeQue = $('#notEntityCategoryItemTypeQue')
				.combobox({
					valueField : 'id',
					textField : 'text',
					data : [ {
						"id" : 0,
						"text" : "全部资源类型",
						"selected" : true
					}, {
						"id" : 1,
						"text" : "视频"
					}, {
						"id" : 2,
						"text" : "音频"
					}, {
						"id" : 3,
						"text" : "文本"
					} ],
					onChange : function(newValue, oldValue) {
						notEntityCategoryListtreegridReload();
					}
				});

		notEntityCategoryListtreegrid = $('#notEntityCategoryList').treegrid({
			url : '${baseUrl}/category/list',
			pagination : true,
			sortName : 'sort,updateTime,ckey',
			sortOrder : 'desc,desc,asc',
			multiSort : true,
			//remoteSort : false,
			// 			rownumbers : true,

			queryParams : notEntityCategoryQueryParams,
			singleSelect : true,
			idField : 'id',
			treeField : 'text',
			lines : true,
			striped : true,
			pageSize : 20,
			fitColumns : true,
			toolbar : '#notEntityCategoryListToolBars',
			onLoadSuccess : function() {
				notEntityCategoryEditRow = undefined;
			},
			onBeforeSelect : function(row) {
				if (notEntityCategoryEditRow) {
					return false;
				}
			},
			onBeforeEdit : function(row) {
				if (notEntityCategoryEditRow == undefined) {
					notEntityCategoryEditRow = row;
				}
			},
			onAfterEdit : function(row, changes) {
				notEntityCategoryEditRow = undefined;
			},
			onCancelEdit : function(row) {
				notEntityCategoryEditRow = undefined;
			},
			// 			nowrap : false,
			columns : [ [ {
				field : 'id',
				title : 'ID',
				align : 'center',
				width : 60
			}, {
				field : 'text',
				title : '名称',
				width : 249,
				editor : {
					type : 'text',
					options : {
						required : true
					}
				}
			}, {
				field : 'ckey',
				title : '类型KEY',
				align : 'center',
				width : 100,
				sortable : true
			}, {
				field : 'itemTypeMsg',
				title : '资源类型',
				align : 'center',
				width : 100
			}, {
				field : 'createTime',
				title : '创建时间',
				width : 100,
				align : 'left'
			}, {
				field : 'updateTime',
				title : '更新时间',
				width : 100,
				align : 'left',
				sortable : true
			}, {
				field : 'createUser',
				title : '创建者',
				width : 60,
				align : 'center',
				formatter : function(value, row, index) {
					if (row.createUser) {
						return row.createUser.nickname;
					} else {
						return value;
					}
				}
			}, {
				field : 'entity',
				title : '分类实体',
				width : 80,
				align : 'center',
				formatter : function(value, row, index) {
					return value;
				}
			}, {
				field : 'sort',
				title : '排序',
				width : 60,
				align : 'center',
				sortable : true,
				editor : {
					type : 'numberbox',
					options : {
						precision : 2,
						required : true
					}
				}
			} ] ],
			onDblClickRow : function(index, row) {
				$.ajax({
					data : {
						id : row.id
					},
					async : false,
					url : baseUrl + '/notEntityCategory/info.html',
					success : function(html) {
						$('body').append(html);
					}
				});

			}
		});

		notEntityCategoryListtreegrid.treegrid('getPager').pagination(
				{
					buttons : [ {
						iconCls : 'fa fa-2x fa-location-arrow',
						handler : function() {
							notEntityCategoryListtreegrid.treegrid('gotoPage',
									notEntityCategoryListtreegrid.treegrid(
											'getPager').find('.pagination-num')
											.val());
						}
					} ]
				});

	});

	function notEntityCategoryList_append() {

		$.ajax({
			url : baseUrl + '/category/add.html?entity=false',
			success : function(html) {
				$('body').append(html);
			}
		});
	}
	function notEntityCategoryList_edit() {
		var notEntityCategorySelect = notEntityCategoryListtreegrid
				.treegrid('getSelected');
		if (notEntityCategoryEditRow != undefined
				&& notEntityCategoryEditRow.id != notEntityCategorySelect.id) {
			$.messager.alert('提示', '请完成已开启编辑行', 'warning');
			return;
		}

		// 		if (notEntityCategoryListtreegrid.treegrid('getLevel',
		// 				notEntityCategorySelect.id) != 1) {
		// 			return;
		// 		}

		if (notEntityCategorySelect) {
			notEntityCategoryListtreegrid.treegrid('beginEdit',
					notEntityCategorySelect.id);
		}
	}

	function notEntityCategoryList_undo() {
		if (notEntityCategoryEditRow != undefined) {
			notEntityCategoryListtreegrid.treegrid('cancelEdit',
					notEntityCategoryEditRow.id);
		}
	}

	function notEntityCategoryList_accept() {
		if (notEntityCategoryEditRow != undefined) {

			if (!notEntityCategoryListtreegrid.treegrid('validateRow',
					notEntityCategoryEditRow.id)) {
				return;
			}

			//验证标题
			var _edTitle = notEntityCategoryListtreegrid.treegrid('getEditor',
					{
						id : notEntityCategoryEditRow.id,
						field : 'text'
					});

			var _Title = $(_edTitle.target).val();

			if (!_Title || _Title.trim() == '') {
				$.messager.alert('提示', '名称不能为空', 'warning');
				return;
			}

			//获取排序
			var _edSort = notEntityCategoryListtreegrid.treegrid('getEditor', {
				id : notEntityCategoryEditRow.id,
				field : 'sort'
			});

			var _Sort = $(_edSort.target).val();

			var _Data = {
				id : notEntityCategoryEditRow.id,
				name : _Title,
				sort : _Sort
			};

			$.ajax({
				data : _Data,
				async : false,
				type : 'POST',
				url : baseUrl + '/category/update',
				success : function(data) {
					if (data.responseCode == '000') {

						notEntityCategoryListtreegrid.treegrid('endEdit',
								notEntityCategoryEditRow.id);

						$.messager.alert('提示', '修改成功', 'info');
					} else {
						$.messager.alert('提示', data.responseMessage, 'error');
					}
				}
			});

		}
	}

	function notEntityCategoryList_notEntityCategoryListPage() {
		var notEntityCategorySelect = notEntityCategoryListtreegrid
				.treegrid('getSelected');
		if (notEntityCategorySelect) {
			$.ajax({
				data : {
					itemId : notEntityCategorySelect.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/notEntityCategory/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function notEntityCategoryList_tagListPage() {

		var notEntityCategorySelect = notEntityCategoryListtreegrid
				.treegrid('getSelected');
		if (notEntityCategorySelect) {
			$.ajax({
				data : {
					itemId : notEntityCategorySelect.id,
					itemType : 1
				},
				async : false,
				url : baseUrl + '/tag/set.html',
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}

	function notEntityCategoryList_remove() {
		var notEntityCategorySelect = notEntityCategoryListtreegrid
				.treegrid('getSelected');
		if (notEntityCategorySelect) {
			$.messager.confirm('确认', '您确认要删除么吗？', function(r) {
				if (r) {
					notEntityCategoryList_remove2(notEntityCategorySelect);
				}
			});
		}
	}

	function notEntityCategoryList_remove2(notEntityCategorySelect, force) {
		var _Data = {
			ids : [ notEntityCategorySelect.id ],
			entity : notEntityCategorySelect.entity == '是' ? true : false,
			force : false
		};
		if (force) {
			_Data.force = force;
		}

		$.ajax({
			data : _Data,
			type : "post",
			async : false,
			url : baseUrl + '/category/delete',
			success : function(data) {
				if (data.responseCode == '000') {
					$.messager.alert('提示', "删除成功!", 'info');

					notEntityCategoryListtreegrid.treegrid('remove',
							notEntityCategorySelect.id);

				} else if (data.responseCode == '023') {
					$.messager.confirm('确认强制删除',
							'<span style="color:red;">强制删除会解除所有关联资源关系</span>',
							function(fr) {
								if (fr) {
									notEntityCategoryList_remove2(
											notEntityCategorySelect, true);
								}
							})
				} else {
					$.messager.alert('提示', data.responseMessage, 'error');
				}
			}
		});
	}

	function notEntityCategoryChildList_config() {
		var notEntityCategorySelect = notEntityCategoryListtreegrid
				.treegrid('getSelected');

		if (notEntityCategorySelect
				&& notEntityCategoryListtreegrid.treegrid('getLevel',
						notEntityCategorySelect.id) == 1) {
			$.ajax({
				async : false,
				url : baseUrl + '/category/children-config.html?id='
						+ notEntityCategorySelect.id,
				success : function(html) {
					$('body').append(html);
				}
			});
		}
	}
</script>
